package org.grayrabbit.cms.publish.generate;

import java.util.ArrayList;
import java.util.List;

import org.grayrabbit.cms.publish.api.Publishable;



public class TopicMission extends Mission{
	private int currentTopicIndex;
	private int currentTopicListIndex;
	private int currentContentIndex;

	public int getCurrentTopicIndex() {
		return currentTopicIndex;
	}

	public void setCurrentTopicIndex(int currentTopicIndex) {
		this.currentTopicIndex = currentTopicIndex;
	}

	public int getCurrentTopicListIndex() {
		return currentTopicListIndex;
	}

	public void setCurrentTopicListIndex(int currentTopicListIndex) {
		this.currentTopicListIndex = currentTopicListIndex;
	}

	public int getCurrentContentIndex() {
		return currentContentIndex;
	}

	public void setCurrentContentIndex(int currentContentIndex) {
		this.currentContentIndex = currentContentIndex;
	}

	public boolean hasNext() {
		return currentTopicIndex<this.getTopicNum()/2
		||currentTopicListIndex<this.getTopicNum()/2
		||currentContentIndex<this.getContentNum();
	}

	public void remove() {
		
	}

	@Override
	public GenTask next() {
		if(currentTopicIndex<this.getTopicNum()/2){
			GenTask task = new GenTask();
			task.setList(getList());
			task.setType(Publishable.TOPIC_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(getList().size()==0){
				currentTopicIndex = this.getTopicNum()/2;
			}else{
				currentTopicIndex+=getList().size();
			}
			return task;
		}else if(currentTopicListIndex<this.getTopicNum()/2){
			GenTask task = new GenTask();
			task.setList(getList());
			task.setType(Publishable.TOPIC_LIST_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(getList().size()==0){
				currentTopicListIndex = this.getTopicNum()/2;
			}else{
				currentTopicListIndex+=getList().size();
			}
			return task;
		}else if(currentContentIndex<this.getContentNum()){
			List<String> contentList = new ArrayList<String>();
			if(Publishable.ALL_PUBLISH.equals(this.getModelType())){
				contentList = generatable.findContentIdsByTopicId(getList(),currentContentIndex, MAX);
			}else{
				contentList = generatable.findMoreContentIdsByTopicId(getList(),currentContentIndex, MAX);
			}
			GenTask task = new GenTask();
			task.setList(contentList);
			task.setType(Publishable.CONTENT_GEN);
			task.setMission(this);
			task.setAdmin(getAdmin());
			if(contentList.size()==0){
				currentContentIndex = this.getContentNum();
			}else{
				currentContentIndex+=contentList.size();
			}
			return task;
		}
		return null;
	}
}